package com.xm.shop.app.api.controller;

import com.xm.shop.pojo.entity.Cart;
import com.xm.shop.pojo.qo.CartAddQO;
import com.xm.shop.pojo.qo.CartUpdateQO;
import com.xm.shop.pojo.vo.RespResult;
import com.xm.shop.service.CartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ludi
 * @date:2020/8/31 9:08
 * @description:购物车业务控制层
 */
@Controller
@RestController
@RequestMapping("/cart")
@Api(tags = "购物车业务接口")
public class CartController extends BaseController{
    @Autowired
    private CartService cartService;

    @GetMapping("/selectByUserId")
    @ApiOperation("查看购物车")
    public RespResult selectByUserId(Integer userId){
        List<Cart> list = cartService.selectByUserId(userId);
        return returnRespWithSuccess(list);
    }

    @PostMapping("/addCart")
    @ApiOperation("添加购物车")
    public RespResult addCart(@RequestBody CartAddQO cartAddQO){
        System.out.println("cartAddQO:"+cartAddQO);
        int value = cartService.addCart(cartAddQO);
        return returnRespWithSuccess(value);
    }

    @PostMapping("/updateCart")
    @ApiOperation("修改购物车")
    public RespResult updateCart(@RequestBody CartUpdateQO cartUpdateQO){
//        System.out.println("cartUpdateQO:"+cartUpdateQO);
        int value = cartService.updateCart(cartUpdateQO);
        return returnRespWithSuccess(value);
    }

    @GetMapping("/deleteCartByCartId")
    @ApiOperation("删除一条购物车")
    public RespResult deleteCartByCartId(Integer cartId){
        int value = cartService.deleteByCartId(cartId);
        return returnRespWithSuccess(value);
    }

    @GetMapping("/deleteAllCartByUserId")
    @ApiOperation("清空购物车")
    public RespResult deleteAllCartByUserId(Integer userId){
        System.out.println("userId:"+userId);
        int value = cartService.deleteAllByUserId(userId);
        return returnRespWithSuccess(value);
    }

}
